import json
import os
from typing import Optional, Dict, Any

import pandas as pd
from openpyxl.reader.excel import load_workbook


def readFile(url):
    # read() - 读取所有
    try:
        with open(url, 'r', encoding='utf-8') as file:
            json_data = json.load(file)
    except FileNotFoundError:
        print("错误: 文件未找到!")
        # pass
    finally:
        file.close()
    return json_data


if __name__ == '__main__':
    # 获取当前脚本所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    parent_dir = os.path.dirname(current_dir)
    # 构建目标文件路径
    target_file = os.path.join(parent_dir, 'data', 'json_test.json')

    # 读取文件内容
    json_data = readFile(target_file)

    # 解析json数据
    json_array = []

    for hit in json_data:
        print(hit.get("key"))
        translationsDict = {}
        entry = {
            "key": hit.get("key"),
            "translations": translationsDict
        }
        for trans in hit["translations"]:
            translationsDict[trans.get("language")] = trans.get("value")

        print(entry)
